from enum import Enum
from typing import List


class RedisKey(object):
    """
    redis 各个变量名
    """
    taskinfo_save_setting = "taskinfo_save_setting"
    taskinfo = "taskinfo"


class SQLTable(object):
    journal_list = "journal_list"
    journal_issue = "journal_issue"
    journal_article = "journal_article"
    journal_home = "journal_home"
    taskinfo = "taskinfo"
    taskinfo_save_setting = "taskinfo_save_setting"
    err_analysis = "err_analysis"
    err_except_data = "err_except_data"
    # ########### paper
    paper_days = "paper_days"
    paper_page = "paper_page"
    paper_article = "paper_article"


class CoreSqlValue(object):
    insert_ig_it = "insert ignore into"
    insert_it = "insert into"
    replace_it = "replace into"


class IsHasEnum(Enum):
    NORMAL = "normal"
    LATEST = "latest"


class StateNode(object):
    # 初始化一个节点
    def __init__(self, node_type, val="", l_d_info=[]):
        self.node_type = node_type
        self.val = val  # 节点值
        self.l_d_info = l_d_info  # 其他信息
        self.l_child = []  # 子节点列表

    # 添加子节点
    def add_child(self, node):
        self.l_child.append(node)

    def get_child(self):
        return self.l_child

    def get_l_d_info(self):
        return self.l_d_info

    def get_rsult(self, pm):
        if self.node_type == "top":
            for node in self.l_child:
                node.get_rsult(pm)
            return True
        elif self.node_type == "is_true":
            lists = []
            for node in self.l_child:
                lists.append(node.get_rsult(pm))
            if all(lists) and lists:
                if self.val != "":
                    pm.set_sql_state({self.val: 1}, is_update=True)
            else:
                if self.val != "":
                    pm.set_sql_state({self.val: 0}, is_update=True)
        elif self.node_type == "state":
            return self.get_state(pm)
        else:
            raise Exception("传入参数错误")

    def get_state(self, pm):
        pm_is_his = pm.get_is_his()
        if all([eval(pm_is_his.get(it_d["keys"], "False")) for it_d in self.l_d_info]) and self.l_d_info:
            pm.set_sql_state({self.val: 1}, is_update=True)
            return True
        else:
            state = 0
            is_sucess = True
            state_dicts = {}
            # 停止在这里面实现失败+1 在外面做 20210608
            # update_no_placeholder = {}
            for it_d in self.l_d_info:
                key_state = it_d["state_dicts"]["key_state"]
                # f_key = it_d["state_dicts"]["f_key"]
                # update_no_placeholder[f_key] = f_key + "+1"
                key = it_d["keys"]
                err_info = pm.get_down_err_info().get(key, None)
                state_dicts["err_msg"] = str(err_info)
                if err_info is not None and isinstance(err_info, dict):
                    if err_info.get("code", 0) != 0:
                        temp_state = int(err_info["code"])
                        if str(temp_state) == "200":
                            continue
                        if str(temp_state) in key_state:
                            # 表示状态码允许通过
                            state = temp_state
                        else:
                            state = temp_state
                            is_sucess = False
                            break
            state_dicts[self.val] = state
            pm.set_sql_state(state_dicts, is_update=True)
            # pm.set_update_no_placeholder(update_no_placeholder, is_update=True)
            return is_sucess
